(function () {

    //获取显示页面数据 服务器分页
    var serverData = {};
    serverData.key='';
    serverData.superior=jQuery('#superior').val();
    var serverTable;
    jQuery(document).ready(function () {

        serverTable = jQuery('#replaceTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/priceBook/replaceDevice/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                    data: null,
                    defaultContent: ""//排序
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var id=data.id
                        var operate ='<input type="checkbox" class="checkchildReplaceDevice">';
                        return operate;
                    }
                },
                {
                    data: 'replacementDevice.deviceName',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 25) ? '<font title="'+data+'">'+data.substring(0,25)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'replacementDevice.type',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'replacementDevice.material',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'superior'
                },
                {
                    data: 'price'
                }
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
        
        //删除备品备件
        jQuery('.delBtn').on('click', function () {
            var selects=getSelect("#replaceTable",1);
            var url=window.rootPath+"/ui/priceBook/replaceDevice/delReplaceDevice";
            delSelect(selects,url,serverTable)
        });
       

        //编辑销售人员属性，添加属性
        jQuery('.editBtn').on('click', function () {
            var selects=getSelect("#replaceTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能编辑一条记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要编辑的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
	            jQuery('#id').val(selects[0].id);
	            jQuery('#replaceId').val(selects[0].replacementDevice.id);
	            jQuery('#reDeviceName').val(selects[0].replacementDevice.deviceName);
	            jQuery('#reDeviceType').val(selects[0].replacementDevice.type);
	            jQuery('#reMaterial').val(selects[0].replacementDevice.material);
	            jQuery('#superior').val(selects[0].superior);
	            jQuery('#createTime').val(selects[0].createTime);
	            jQuery('#price').val(selects[0].price);
	            jQuery('#replaceDeviceModal').modal('show');
            };
        });
        //列表中的全选
        $("#checkAllReplaceDevice").click(function(){
            var check = $(this).prop("checked");
            $(".checkchildReplaceDevice").prop("checked", check);
        });
        //编辑价格
        var $btn = jQuery('.saveBtn').on('click', function () {
            if(jQuery("#replaceDeviceEditForm") && !jQuery("#replaceDeviceEditForm").Validform({tiptype:3, showAllError:true}).check(false)){
                return;
            };
            var id=jQuery("#id").val();
            var replaceId=jQuery("#replaceId").val();
            var createTime=jQuery('#createTime').val();
            var superior=jQuery('#superior').val();
            var price=jQuery('#price').val();
            var data={
                    "id":id,
                    "replacementDevice":{
                    	"id":replaceId
                    },
            		"createTime":createTime,
            		"superior":superior,
            		"price":price
                };
                var url=window.rootPath+'/ui/priceBook/replaceDevice/save';
                jQuery.ajax({
                    url:encodeURI(url),
                    type:'post',
                    data:JSON.stringify(data),
                    dataType:'json',
                    contentType:'application/json;charset=utf-8',
                    success: function () {
                        jQuery('#replaceDeviceModal').modal('hide');
                        serverTable.draw(false);
                    }
                });
        });

        //查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });

        //EXCEL导入
        jQuery('.btnImport').on("click",function()
        {
            fileUpload();
        });

        /*  //导入完成后刷新
         jQuery('.completeUploadBtn').on("click",function()
         {
         window.location.href=root+"/ui/device";
         });*/
        /**
         * 文件上传插件
         */
        var fileUpload = function() {
            var root = window.rootPath;
            WebFilesUploader(
                {
                    _webuploader : {
                        server : root + '/ui/priceBook/replaceDevice/import',
                        chunked:false
                    }

                }, function(jsons) {
                    window.location.href=root+'/ui/priceBook/replaceDevice';
                });


        };
    });
})();
